#! /bin/bash
#flower
#用于安装jdk1.8 及tomcat 
#同时生成用于jenkins 使用的部署脚本
#用户安装脚本生成的临时目录
temp=/webinstalltemp
jdkpath=/usr/java/
tomcatpath=/home/usertomcat/tomcat/
linuxservice=/usr/lib/systemd/system/
cd /
mkdir -p $temp
cd $temp
echo '开始安装jdk1.8'
curl -O https://mdreamworld.cn/file/jdk1.8.0_45.tar.gz
#创建
mkdir -p $jdkpath
cp jdk1.8.0_45.tar.gz $jdkpath
cd $jdkpath
tar -zxf jdk1.8.0_45.tar.gz
#追加环境变量
echo "export JAVA_HOME=/usr/java/jdk1.8.0_45" >> /etc/profile
echo "export JRE_HOME=/usr/java/jdk1.8.0_45/jre" >> /etc/profile
echo "JAVA_HOME=/usr/java/jdk1.8.0_45" >> /etc/profile
echo "PATH=\$JAVA_HOME/bin:\$PATH" >> /etc/profile
echo "CLASSPATH=.:\$JAVA_HOME/jre/lib/ext:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
echo "export PATH JAVA_HOME CLASSPATH" >> /etc/profile
#刷新环境变量
source /etc/profile
echo '-------------jdk环境变量------------------'
java -version
echo '-------------jdk环境变量------------------'
echo '开始安装tomcat'
cd $temp
curl -O https://mdreamworld.cn/file/apache-tomcat-9.0.41.tar.gz
mkdir -p $tomcatpath
cp  apache-tomcat-9.0.41.tar.gz $tomcatpath
cd $tomcatpath
tar -zxf apache-tomcat-9.0.41.tar.gz
mv apache-tomcat-9.0.41 tomcat9
#配置tomcat开机自启 创建usertomcat用户
useradd usertomcat
chown -R usertomcat:usertomcat $tomcatpath
cd $linuxservice
echo "[Unit]" >> tomcat9.service
echo "Description=Tomcat" >> tomcat9.service
echo "After=syslog.target network.target remote-fs.target nss-lookup.target" >> tomcat9.service
echo "[Service]" >> tomcat9.service
echo "User=usertomcat" >> tomcat9.service
echo "Type=oneshot" >> tomcat9.service
echo "ExecStart=/home/usertomcat/tomcat/tomcat9/bin/startup.sh" >> tomcat9.service
echo "ExecStop=/home/usertomcat/tomcat/tomcat9/bin/shutdown.sh" >> tomcat9.service
echo "ExecReload=/bin/kill -s HUP \$MAINPID" >> tomcat9.service
echo "RemainAfterExit=yes" >> tomcat9.service
echo "[Install]" >> tomcat9.service
echo "WantedBy=multi-user.target" >> tomcat9.service
#配置解除
cd $tomcatpath
cd tomcat9/bin/
sed -e '23 i\export JAVA_HOME=/usr/java/jdk1.8.0_45' -i setclasspath.sh
sed -e '24 i\export JRE_HOME=/usr/java/jdk1.8.0_45/jre' -i setclasspath.sh
#修改setclasspath.sh 文件结束
echo '修改setclasspath.sh 文件结束'
systemctl enable tomcat9.service
echo '设置tomcat9自启结束'
systemctl start tomcat9.service
echo '放行防火墙端口号8080'
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
echo '放行防火墙端口号8080结束'
#创建多机Jenkins部署脚本
cd $tomcatpath
mkdir deploy
cd deploy
echo "DATE=\$(date +%Y%m%d%H%M%S)" >> deploy.sh
echo "DIRTEMP=/home/tomcat/deploy" >> deploy.sh
echo "DIR=/home/usertomcat/tomcat/tomcat9/webapps" >> deploy.sh
echo "backup=/home/usertomcat/tomcat/backup" >> deploy.sh
echo "projectName=ROOT.war" >> deploy.sh
echo "cd \$DIR" >> deploy.sh
echo "cd if [ ! -d \$backup ];then" >> deploy.sh
echo " mkdir -p \$backup" >> deploy.sh
echo "fi" >> deploy.sh
echo "mv \$projectName \$backup/\$projectName\$DATE" >> deploy.sh
echo "cd \$DIRTEMP" >> deploy.sh
echo "mv \$projectName \$DIR/\$projectName" >> deploy.sh
chmod 777 deploy.sh
echo "webinstalltemp不再脚本中执行删除,请自行删除 位置/webinstalltemp"
echo "如果jenkins 部署的话  免密登录后scp war 到DIRTEMP目录  并执行deploy.sh即可"
echo "jenkins自动化目录/home/tomcat/deploy"
echo "jenkins自动化执行脚本位置:/home/usertomcat/tomcat/deploy/deploy.sh"
echo "不需要自动化,可删除/home/tomcat/deploy目录"
echo "jdk位置:/usr/java/jdk1.8.0_45"
echo "tomcat webapp位置:/home/usertomcat/tomcat/tomcat9/webapps"
echo "环境初始化完成"
#如果jenkins 部署的话  免密登录后scp war 到DIRTEMP目录  并执行deploy.sh即可
#创建结束 删除多余文件